﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Mail;
using System.Text;

namespace InnovTech.Util.Net
{
    public static class EmailUtil
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender">The Email address of the sender</param>
        /// <param name="pswd">The Email password of the sender</param>
        /// <param name="senderDisplayName">The display name of the sender</param>
        /// <param name="senderHostIP">The host IP for the Email server of the sender</param>
        /// <param name="senderHostPort">The host port for the Email server of the sender</param>
        /// <param name="receiverList">The Email address list of receives </param>
        /// <param name="ccList">The Email address list for cc</param>
        /// <param name="bccList">The Email address list for bcc</param>
        /// <param name="isBodyHtml">Checks whether using html for the message body</param>
        /// <param name="subject">The Email subject</param>
        /// <param name="msgBody"></param>
        /// <param name="encoding"></param>
        /// <param name="attachments"></param>
        /// <returns></returns>
        public static void SendMail(string sender,
                                    string pswd,
                                    string senderDisplayName, 
                                    string senderHostIP, 
                                    int senderHostPort, 
                                    IEnumerable<string> receiverList,
                                    IEnumerable<string> ccList,
                                    IEnumerable<string> bccList, 
                                    bool isBodyHtml, 
                                    string subject,
                                    object msgBody,
                                    Encoding encoding,
                                    IEnumerable<string> attachments)
        {
            SmtpClient client = new SmtpClient();
            MailMessage msg = new MailMessage();
            try
            {
                MailAddress addrFrom = new MailAddress(sender, senderDisplayName);

                client.Host = senderHostIP;
                client.Port = senderHostPort;


                msg.Subject = subject;
                msg.From = addrFrom;
                foreach (string toAddr in receiverList)
                {
                    msg.To.Add(toAddr);
                }
                foreach (string ccAddr in ccList)
                {
                    msg.CC.Add(ccAddr);
                }
                foreach (string bccAddr in bccList)
                {
                    msg.Bcc.Add(bccAddr);
                }

                msg.IsBodyHtml = isBodyHtml;
                msg.Body = msgBody.ToString();
                msg.BodyEncoding = encoding;

                foreach (string att in attachments)
                {
                    msg.Attachments.Add(new Attachment(att));
                }

                client.EnableSsl = true;
                client.UseDefaultCredentials = false;
                client.Credentials = new System.Net.NetworkCredential(sender, pswd);
                client.Send(msg);
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                client = null;
                msg = null;
            }
        }

        public static void SendMail(EmailInfo senderInfo,
                                    IEnumerable<string> receiverList,
                                    IEnumerable<string> ccList,
                                    IEnumerable<string> bccList,
                                    bool isBodyHtml,
                                    string subject,
                                    object msgBody,
                                    Encoding encoding,
                                    IEnumerable<string> attachments)
        {
            SendMail(senderInfo.Address, senderInfo.Pswd, senderInfo.DisplayName, senderInfo.ServerHostIP, senderInfo.ServerHostPort, 
                receiverList, ccList, bccList, isBodyHtml, subject, msgBody, encoding, attachments); 
        }

        public static void SendMail(EmailInfo senderInfo, string receiver, string subject, object msgBody, params string[] attachment)
        {
            SendMail(senderInfo, new string[] { receiver }, new string[0], new string[0], false, subject, msgBody, Encoding.UTF8, attachment);
        }

        public static void SendMail(EmailInfo senderInfo, string receiver, object msgBody, params string[] attachment)
        {
            SendMail(senderInfo, receiver, "temp", msgBody, attachment);
        }
    }

    [Serializable]
    public class EmailInfo
    {
        public string Address { get; set; }
        public string Pswd { get; set; }
        public string DisplayName { get; set; }
        public string ServerHostIP { get; set; }
        public int ServerHostPort { get; set; }
    }
}
